#!/bin/bash

# Build script for YOLOv8 OpenVINO Go Service on Linux

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${GREEN}=== YOLOv8 OpenVINO Go Service Build Script ===${NC}"

# Check if go is installed
if ! command -v go &> /dev/null; then
    echo -e "${RED}Error: Go is not installed${NC}"
    exit 1
fi

# Get script directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
OUTPUT_DIR="${SCRIPT_DIR}/bin"

# Create output directory
mkdir -p "${OUTPUT_DIR}"

# Download dependencies
echo -e "${YELLOW}Downloading dependencies...${NC}"
cd "${SCRIPT_DIR}"
go mod download

# Build
echo -e "${YELLOW}Building Go service...${NC}"
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
    -o "${OUTPUT_DIR}/goyolo-service" \
    -ldflags="-s -w" \
    .

echo -e "${GREEN}Build completed successfully!${NC}"
echo -e "${GREEN}Binary: ${OUTPUT_DIR}/goyolo-service${NC}"
echo -e "${YELLOW}Usage:${NC}"
echo -e "  PORT=8080 MODEL_PATH=/path/to/model.xml CPP_BINARY_PATH=/path/to/detect_service ${OUTPUT_DIR}/goyolo-service"

